package com.roc.template.base;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.roc.template.cloud.CloudConfig;
import com.roc.template.cloud.constant.AccessTokenType;
import com.roc.template.cloud.factory.AccessTokenFactory;
import com.roc.template.cloud.factory.impl.AppAccessToken;
import com.roc.template.cloud.factory.impl.ResGroupAccessToken;
import com.roc.template.redis.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName BaseController
 * @Description: TODO
 * @Author Administrator
 * @Date 2020/7/27 17 02
 * @Version V1.0
 **/
@Component
public class BaseController {

    protected static Logger logger = LoggerFactory.getLogger(BaseController.class);

    @Autowired
    private CloudConfig cloudConfig;

    @Autowired
    private static CloudConfig cloudConfigStatic;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private static RedisUtil redisUtilStatic;

    @PostConstruct
    public void  init() {
        cloudConfigStatic=cloudConfig;
        redisUtilStatic=redisUtil;
    }


    /**
     * 获取用户信息
     * @param request
     * @return
     */
    public static UserVo getUserInfo(HttpServletRequest request) throws Exception {

        UserVo userVo = new UserVo();

        logger.info(request.getRequestURI());
        String ticket = request.getParameter("ticket");
        if(ticket!=null) {
            if(redisUtilStatic.exists(ticket)) {
                String s = redisUtilStatic.get(ticket);
                userVo = JSON.parseObject(s, UserVo.class);
                return userVo;
            }
            Map<String, String> param = new HashMap<>();
            param.put("ticket", ticket);
            AppAccessToken appAccessToken = (AppAccessToken) AccessTokenFactory.getInstance(AccessTokenType.APP,cloudConfigStatic);
            JSONObject userinfo = appAccessToken.acquirecontext(param);
            String openid = userinfo.getString("openid");
            String username = userinfo.getString("username");
            String eid =userinfo.getString("eid");
            Map<String, Object> params = new HashMap<>();
            params.put("eid", eid);
            params.put("type", 1);
            params.put("array", new String[]{openid});
            ResGroupAccessToken resGroupAccessToken  = (ResGroupAccessToken) AccessTokenFactory.getInstance(AccessTokenType.RESGROUPSECRET,cloudConfigStatic);
            JSONArray userInfos = resGroupAccessToken.getUserInfo(params);
            String department = userInfos.getJSONObject(0).getString("department");
            String photoUrl = userInfos.getJSONObject(0).getString("photoUrl");
            userVo.setOpenId(openid);
            userVo.setUsername(username);
            userVo.setDepartment(department);
            userVo.setPhotoUrl(photoUrl);
            redisUtilStatic.set(ticket, JSON.toJSONString(userVo), 60 * 30L);
            return userVo;
        }else {
            userVo.setOpenId("aa");
            return userVo;
        }
    }
}
